package cn.myfirefly.moming.integrate.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.integrate.dao.WorkCalendarDao;
import cn.myfirefly.moming.integrate.entity.MMWorkCalendar;
import cn.myfirefly.moming.integrate.model.WorkCalendarDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.DateUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class WorkCalendarService {

	@Autowired
	private WorkCalendarDao workCalendarDao;
	
	public WorkCalendarDto getWorkDate(Date dateValue) {
		WorkCalendarDto dto = null;
		MMWorkCalendar  workCalendar = workCalendarDao.getWorkDate(dateValue);
		if (workCalendar != null) {
			dto = new WorkCalendarDto();
			BeanUtil.copyBeans(workCalendar, dto);
		}
		return dto;
	}
	
	public String getWorkDates() {
		StringBuffer ds = new StringBuffer("");
		List<MMWorkCalendar> list = workCalendarDao.getAll();
		if (list != null) {
			for (MMWorkCalendar workCalendar : list) {
				ds.append(",'").append(DateUtil.formatDate(workCalendar.getDateValue()))
					.append("|").append(workCalendar.getDateType()).append("'");
			}
		}
		if (ds.length() > 0) {
			return ds.substring(1);
		}
		return ds.toString();
	}
	
	public String save(WorkCalendarDto dto) {
		MMWorkCalendar workCalendar = workCalendarDao.getWorkDate(dto.getDateValue());
		if (StrUtil.notBlank(dto.getWorkCalendarSn())) {
			// 更新
			if (workCalendar == null) {
				return "日期信息不存在，请刷新页面后重试。";
			}
		} else {
			// 新增
			if (workCalendar != null) {
				return "日期信息已存在。";
			}
			workCalendar = new MMWorkCalendar();
		}
		BeanUtil.copyBeans(dto, workCalendar);
		workCalendarDao.save(workCalendar);
		return Const.ResultCode.SUCCESS;
	}
	
	public String del(Integer sn) {
		MMWorkCalendar workCalendar = workCalendarDao.get(sn);
		if (workCalendar == null) {
			return "日期信息不存在，请刷新页面后重试。";
		}
		workCalendarDao.del(workCalendar);
		return Const.ResultCode.SUCCESS;
	}
}
